#include "conf/znx_http_site_conf_mgr.h"

int main()
{   
    if (!znx_http_main_conf_parse("/tmp/http/main.yaml")) {
        if (g_http_main_conf != NULL) {
            ZNX_DEF_LOG_FATAL(NULL, "parse %s failed, err: %v",
                "/tmp/http/main.ymal", &g_http_main_conf->err);
        } else {
            ZNX_DEF_LOG_FATAL(NULL, "parse %v failed, err: unknow",
                "/tmp/http/main.ymal");
        }
    }

    znx_g_http_site_mgr_create();
    znx_g_http_site_mgr_init("/tmp/http/sites_enabled/");

    znx_http_addr_site_conf_t   *addr_site_conf;
    znx_str_t                   host;
    znx_address_t               listen_address;
    znx_bool_t                  not_found = ZNX_FALSE;
    char                        *err = NULL;
    int                         port = 0;

    bzero(&listen_address, sizeof(znx_address_t));
    znx_address_parser((u_char *)":8888", sizeof(":8888")-1, &listen_address, &port);

    ZNX_STR_SET(&host, "www.a.com");
    addr_site_conf = znx_http_addr_site_lookup(&host, &listen_address, &not_found, &err);
    if (addr_site_conf == NULL ||
        addr_site_conf->site_conf->server_name.len != sizeof("www.a.com")-1 ||
        znx_strncmp(addr_site_conf->site_conf->server_name.data,
            "www.a.com", sizeof("www.a.com") - 1) != 0)
    {
        ZNX_DEF_LOG_FATAL(NULL, "unexpect");
    }
    znx_http_addr_site_conf_free(addr_site_conf);

    ZNX_STR_SET(&host, "aaa.a.com");
    addr_site_conf = znx_http_addr_site_lookup(&host, &listen_address, &not_found, &err);
    if (addr_site_conf == NULL ||
        addr_site_conf->site_conf->server_name.len != sizeof("*.a.com")-1 ||
        znx_strncmp(addr_site_conf->site_conf->server_name.data,
            "*.a.com", sizeof("*.a.com") - 1) != 0)
    {
        ZNX_DEF_LOG_FATAL(NULL, "unexpect");
    }
    znx_http_addr_site_conf_free(addr_site_conf);

    ZNX_STR_SET(&host, "aaa.a.com");
    addr_site_conf = znx_http_addr_site_lookup(&host, &listen_address, &not_found, &err);
    if (addr_site_conf == NULL ||
        addr_site_conf->site_conf->server_name.len != sizeof("*.a.com")-1 ||
        znx_strncmp(addr_site_conf->site_conf->server_name.data,
            "*.a.com", sizeof("*.a.com") - 1) != 0)
    {
        ZNX_DEF_LOG_FATAL(NULL, "unexpect");
    }
    znx_http_addr_site_conf_free(addr_site_conf);

    ZNX_STR_SET(&host, "aaa.aaa.a.com");
    addr_site_conf = znx_http_addr_site_lookup(&host, &listen_address, &not_found, &err);
    if (addr_site_conf == NULL ||
        addr_site_conf->site_conf->server_name.len != sizeof("*.a.com")-1 ||
        znx_strncmp(addr_site_conf->site_conf->server_name.data,
            "*.a.com", sizeof("*.a.com") - 1) != 0)
    {
        ZNX_DEF_LOG_FATAL(NULL, "unexpect");
    }
    znx_http_addr_site_conf_free(addr_site_conf);

    ZNX_STR_SET(&host, "aaa.aaa.aaa.a.com");
    addr_site_conf = znx_http_addr_site_lookup(&host, &listen_address, &not_found, &err);
    if (addr_site_conf == NULL ||
        addr_site_conf->site_conf->server_name.len != sizeof("*.a.com")-1 ||
        znx_strncmp(addr_site_conf->site_conf->server_name.data,
            "*.a.com", sizeof("*.a.com") - 1) != 0)
    {
        ZNX_DEF_LOG_FATAL(NULL, "unexpect");
    }
    znx_http_addr_site_conf_free(addr_site_conf);


    ZNX_STR_SET(&host, "aaa.aaa.aaa.aaa.a.com");
    addr_site_conf = znx_http_addr_site_lookup(&host, &listen_address, &not_found, &err);
    if (addr_site_conf != NULL) {
        ZNX_DEF_LOG_FATAL(NULL, "unexpect");
    }


    ZNX_STR_SET(&host, "www.c.com");
    addr_site_conf = znx_http_addr_site_lookup(&host, &listen_address, &not_found, &err);
    if (addr_site_conf == NULL ||
        addr_site_conf->site_conf->server_name.len != sizeof("www.c.com")-1 ||
        znx_strncmp(addr_site_conf->site_conf->server_name.data,
            "www.c.com", sizeof("www.c.com") - 1) != 0)
    {
        ZNX_DEF_LOG_FATAL(NULL, "unexpect");
    }
    znx_http_addr_site_conf_free(addr_site_conf);

    ZNX_STR_SET(&host, "www.z.com");
    addr_site_conf = znx_http_addr_site_lookup(&host, &listen_address, &not_found, &err);
    if (addr_site_conf != NULL) {
        ZNX_DEF_LOG_FATAL(NULL, "unexpect");
    }

    znx_g_http_site_mgr_destroy();
}
